package com.mxl.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author MXL
 * @date 2021/10/12
 **/
@Configuration
public class TTLConfig {

    public static final String TTL_QUEUE_NAME = "queue-ttl";
    public static final String TTL_EXCHANGE_NAME = "exchange-ttl";

    @Bean
    Queue ttlQueue(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 1000 * 5);
        return new Queue(TTL_QUEUE_NAME,  true, false, false, args);
//        return new Queue(TTL_QUEUE_NAME);
    }

    @Bean
    DirectExchange ttlExchange(){
        return new DirectExchange(TTL_EXCHANGE_NAME);
    }

    /**
     * 绑定  队列 到 交换机 并使用 routerKey
     *
     * @return
     */
    @Bean
    Binding ttlBinding() {
        return BindingBuilder.bind(ttlQueue()).to(ttlExchange()).with(TTL_QUEUE_NAME);
    }


}
